<style>
	.bt-w-menu p a{
		text-decoration: none;
	}
	.mr10{
		margin-right: 10px;
	}
	.masterslave .master,
	.masterslave .slave{
		padding: 15px;
	}
	.masterslave .header{
		height: 30px;
		margin-bottom: 15px;
	}
	.pull{
		width: 270px;
	    position: absolute;
	    top: 141px;
	    left: 120px;
	    border: 1px solid #c1c1c1cc;
	    border-radius: 4px;
	    background-color: #fefefe;
	}
	.pull li{
		height: 30px;
	    line-height: 30px;
	    padding: 0 10px;
	    cursor: pointer;
	}
	.pull li:hover{
		background-color: #f5f5f5;
	}
	.list-item{
		background-image: url('/static/img/icon-item.png');
		background-repeat: no-repeat;
		background-position: 245px 6px;
		background-size: 15px 15px;
	}


	.Confirmation{
		padding: 30px;
		padding-bottom: 40px;
	}
	.Confirmation h3{
		font-size: 15px;
	    padding-left: 10px;
	    padding-bottom: 15px;
	    margin-bottom: 15px;
	    border-bottom: 1px solid #CECECE;
	}
	.Confirmation li{
		font-size: 15px;
	    margin-bottom: 10px;
	    list-style: decimal;
	    margin-left: 25px;
	}
	.btn_confirm{
		color: #20A53A;
	}
	.btn_confirm:hover{
		color: #1a7b2d;
	}
	.btn_confirm_grup{
	    padding-top: 15px;
	    overflow: hidden;
	    padding-bottom: 15px;
	    border-bottom: 1px solid #ECECEC;
	}
	.btn_confirm_grup .btn{
		height: 40px;
	    padding: 0 25px;
	    line-height: 40px;
	    font-size: 14px;
	    margin-right: 4px;
	    float: right;
	}
	.preview{
		margin-top: 20px;
	}
	.showImages{
		line-height: 30px;
		vertical-align: baseline;
		padding-left: 10px;
	}
	.showImages>input{
		height: 30px;
		border-top-left-radius:3px;
		border-bottom-left-radius: 3px;
		width: 185px;
		margin-right: -1px;
		border: 1px solid #cbcbcb;
		vertical-align: baseline;
		padding-left: 10px;
	}
	.showImages label button{
		border-radius: 0px;
		border-top-right-radius:3px;
		border-bottom-right-radius: 3px;
		vertical-align: baseline;
	}
	.showImages>button{
		margin-left: 25px;
		vertical-align: baseline;
	}


	.box-conter{
		width: 400px;
		margin: 4px 25px;
		height: 150px;
		line-height: 150px;
		text-align: center;
	}
	.box-conter button{
		margin: 15px;
	}
	#ymlist{
		padding: 5px 10px;
		max-height:200px;
		overflow:auto;
		width:270px;
		border:#ccc 1px solid;
		border-radius:3px;
	}


	.set_server{
		padding: 15px 25px;
	}

	.set_server .title{
		height: 35px;
		line-height: 35px;
		font-size: 13px;
		color: #333;
		border-bottom: 0;
	}
	.set_server .progress{
		background-color: #e2e2e2;
		border-radius: 10px;
		height: 15px;
	}
	.set_server .box_server_progress{
		/* padding-top: 25px; */
		line-height: 25px;
		border-top: 1px solid #ecececec;
		padding: 10px 0;
		color: #666;
	}
	.set_server .progress-bar{
		background-color:#4caf50;
		text-align: right;
		padding-right: 5px;
		line-height: 15px;
	}
	.success_server{
		padding: 15px 40px;
		height: 80px;
		font-size: 14px;
		color:#333;
	}
	.success_server img{
		height: 50px;
	}
	.success_server span{
		display: inline-block;
    	margin: 0 10px;
	}
	.masterslave .master_slave_list td span{
		display: block;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		cursor: pointer;
	}
	.dbs_layer_view span{
		display: inline-block;
		width: 100%;
		border:1px solid #ececec;
		padding-left: 10px;
		/* text-align: center; */
		border-bottom: 0;
		padding: 8px;
	}
	.dbs_layer_view span:last-child{
		border-bottom:1px solid #ececec;
	}
	.dbs_layer_view span.th{
		background: #f3f3f3;
	}
	.dbs_layer_view .info_dbs{
		margin-top: 10px;
		color: #333;
	}

	.masterslave .tip{
		position: absolute;
		line-height: 22px;
		color: #777;
		margin: 20px;
		bottom:30px;
	}
	.masterslave_mask{
		width: 100%;
		height: 100%;
		background: rgba(45, 45, 45, 0.7);
		position: absolute;
		left: 0;
		top: 0;
		z-index: 999;
		display: none;
	}
	.masterslave_init{
		position: absolute;
		left: 50%;
		top: 50%;
		margin-top: -90px;
		margin-left: -210px;
		z-index: 999;
		height: 120px;
		border-radius: 3px;
		background: #fff;
		padding: 30px
	}
	.masterslave_init button{
		margin:5px 15px;
	}

	.server_state{
		position: absolute;
		bottom: 0;
		height: 38px;
		line-height: 38px;
		width: 100%;
		padding-right: 20px;
		border-top: 1px solid #ececec;
		background: #f7f5f5;
		color: #777;
		text-align: right;
	}
	.server_state .text{
		cursor: pointer;
	}
	.masterslave .header{
		display: none;
	}
</style>
<div class="masterslave" style="height: 480px;">
	<div class="bt-w-con ml0" style="margin-left:0px;">
		<div class="master" >
			<div class="header">
				<div class="pull-left">
					<button class="btn btn-success btn-sm va0 add_master mr10">配置主服务器</button>
					<button class="btn btn-success btn-sm va0 add_slave mr10">配置从服务器</button>
					<button class="btn btn-success btn-sm va0 add_slave1 mr10" >添加从服务器</button>
					<button class="btn btn-success btn-sm va0 get_key">主服务器密钥</button>
				</div>
			</div>
			<div class="conter divtable">
				<table class="table table-hover">
					<thead>
						<tr>
							<th>主服务器IP</th>
							<th>从服务器IP</th>
							<th>数据库</th>
							<th>SQL状态(Slave_SQL_Running)</th>
							<th>IO状态(Slave_IO_Running)</th>
							<th style="text-align: right;">操作</th>
						</tr>
					</thead>
					<tbody class="master_slave_list"></tbody>
				</table>
			</div>
		</div>
	</div>
	<!-- <div class="masterslave_mask"></div>
	<div class="masterslave_init"></div> -->
	<span class="tip">
		温馨提示：<br>
		1. 如需将该服务器设置为主服务器，请点击“配置为主服务器”<br>
		2. 如需将该服务器设置为从服务器，请点击“配置为从服务器”<br>
		<span style="color: red;">3. 当前MySQL主从复制插件仅支持一主多从,如需其他主从模式，请等待后期插件更新。</span><br>
		4. 如需修复主从，请严格按照提示进行操作<br>
		5. <a class="btlink" target="_blank" href="https://www.bt.cn/bbs/thread-12900-1-1.html">详细使用方法，请点击这里查看教程</a><a href="https://www.bt.cn/bbs/thread-12900-1-1.html" target="_blank" class="bt-ico-ask" style="cursor: pointer;">?</a>
	</span>
	<div class="server_state">当前服务器配置为:&nbsp;&nbsp;<span style="color:#20a53a;" class="text" data-code="0">未配置服务</span></div>
</div>
<script type="text/javascript" src="/static/js/upload.js"></script>
<script>
  var masterSlave = {
		plugin_name:'masterslave',
		ip_reg:/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,
		port_reg:/^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{4}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/,
		db_List:[],
		checkbox_db_list:[],
		timer_index:'',
		loadT:[],
		progress:[],
		// 初始化
		init:function(){
			var _this = this;
			$('.layui-layer-page').width(750);
			// this.getMasterSlave();
			$('.add_slave').click(function(){
				if($('.master_slave_list tr').length > 0) {
					layer.msg('当前主从复制，仅支持单个服务，如需支持多个服务，请等待后期更新。',{icon:0})
					return false;
				}
				masterSlave.loadT[0] = layer.open({
					type: 1,
					closeBtn:2,
					title:'配置从服务器',
					area: '450px', //宽高
					content: '<div class="bt-form pd20" id="MasterFrom" >\
									<div class="line"><span class="tname">主服务器ip</span>\
										<div class="info-r c4"><input class="bt-input-text" type="text" name="slave_ip" placeholder="请输入主服务器ip" value="" style="width:270px"></div>\
									</div>\
									<div class="line"><span class="tname">主服务器密钥</span><div class="info-r c4"><textarea id="mainDomain" class="bt-input-text" name="secret_key"  placeholder="此密钥为主服务器生成密钥" style="width:270px;height:100px;line-height:22px"></textarea></div>\
									</div>\
							</div>',
					success:function(){
						$('#MasterFrom [name="slave_ip"]').focus().unbind();
					},
					btn: ['确定', '取消'],
					yes:function(){
						_this.progress = [];
						var ipVal = $('input[name="slave_ip"]').val();
						var secretVal = $('textarea[name="secret_key"]').val();
						if(!_this.ip_reg.test(ipVal)){
							layer.msg('IP地址格式错误，请重试！');
							return false;
						}else if(secretVal == ''){
							layer.msg('密钥不能为空！');
							return false;
						}
						_this.set_slave({
							master_ip:ipVal,
							keys:secretVal,
							success:function(res){
								if(typeof res != 'object' || res.status == false){
									clearInterval(_this.timer_index);
									layer.close(masterSlave.loadT[1]);
									return false
								}
								layer.msg(res.msg,{icon:1});
								layer.close(masterSlave.loadT[0]);
								layer.close(masterSlave.loadT[1]);
								_this.create_table_list();
								_this.check_m_or_s();
							}
						});
						masterSlave.loadT[1] = layer.open({
							type: 1,
							closeBtn:0,
							title:false,
							area: '350px', //宽高
							content:'<div class="set_server">\
										<div class="title">服务配置中，请稍后<img src="/static/img/ing.gif"></div>\
										<div class="progress"><div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">0%</div></div>\
										<div class="box_server_progress">\
											<div class="item alive">备份数据库进度：0%</div>\
										</div>\
									</div>',
							success:function(){
								_this.timer_index = setInterval(function(){
									_this.get_slave_total_speed(function(res){
										if(res.status != undefined){
											layer.msg(res.msg,{icon:res.status?1:2});
											layer.close(masterSlave.loadT[1]);
											clearInterval(_this.timer_index);
											return false;
										}
										if(res.total == 100) clearInterval(_this.timer_index);
										$('.set_server .progress-bar').css('width',res.total+'%').html(res.total+'%');
										_this.get_slave_speed(function(ress){
											if(ress.status != undefined){
												layer.msg(ress.msg,{icon:ress.status?1:2});
												layer.close(masterSlave.loadT[1]);
												clearInterval(_this.timer_index);
												return false;
											}
											if(_this.progress.length != 0 && _this.progress[_this.progress.length - 1].title == ress.title){
												_this.progress[_this.progress.length - 1] = ress;
											}else{
												if(_this.progress.length != 0) _this.progress[_this.progress.length - 1].progress = 100
												_this.progress.push(ress);
											}
											var _progress_list = '';
											for(var i = 0;i < _this.progress.length;i++){
												_progress_list += '<div class="item">'+  _this.progress[i].title +'进度：'+  _this.progress[i].progress +'%</div>';
											}
											if(ress.progress == 100 && ress.title == "重启数据库"){
												clearInterval(_this.timer_index);
											}
											$('.box_server_progress').html(_progress_list);
										});
									});
								},2000);
							}
						});
					}
				});
			});
			$('.add_master').click(function(){
				if($('.master_slave_list tr').length > 0){
					layer.msg('当前主从复制，仅支持单个服务，如需支持多个服务，请等待后期更新。',{icon:0});
					return false;
				}
				var db_html = '';
				_this.get_dbs(function(res){
					_this.db_List = res;
					masterSlave.loadT[0] = layer.open({
						type: 1,
						closeBtn:2,
						title:'配置主服务器',
						area: '450px', //宽高
						content: '<form class="bt-form pd20" id="MasterFrom" accept-charset="utf-8">\
										<div class="line">\
											<span class="tname">从库ip地址</span>\
											<div class="info-r c4"><input class="bt-input-text" type="text" name="master_ip" placeholder="请输入从服务器ip地址" value="" style="width:270px"></div>\
										</div>\
										<div class="line">\
											<span class="tname">从库端口</span>\
											<div class="info-r c4"><input class="bt-input-text" type="text" name="master_port" placeholder="请输入从服务器数据库端口" value="3306" style="width:270px"></div>\
											<div class="info-r c4" style="line-height: 30px;color:#999"> * Mysql默认端口为3306，如存在变动，请更改。</div>\
										</div>\
										<div class="line">\
											<span class="tname">选择数据库</span>\
											<div class="info-r c4"><ul id="ymlist"</ul></div>\
										</div>\
									</form>',
						btn: ['确定', '取消'],
						yes:function(){
						    layer.confirm('Mysql5.6或以上版本第一次设置可能需要重启数据库！！！<br>是否继续？</br>', {icon: 3, title:'提示'}, function(){
								var ipVal = $('input[name="master_ip"]').val();
								var portVal = $('input[name="master_port"]').val();
								if(!_this.ip_reg.test(ipVal)){
									layer.msg('IP地址格式错误，请重试！');
									return false;
								}else if(!_this.port_reg.test(portVal)){
									layer.msg('数据库端口格式错误，请重试！');
									return false;
								}else if(_this.checkbox_db_list.length == 0){
									layer.msg('数据库不能为空，请选择！');
									return false;
								}
								_this.set_master({
									slave_ip:ipVal,
									slave_port:portVal,
									replicate_dbs:JSON.stringify(_this.checkbox_db_list),
									success:function(res){
										layer.close(masterSlave.loadT[0]);
										masterSlave.loadT[0] = layer.open({
											type:1,
											closeBtn:2,
											skin:'layer_close',
											title:'主服务器密钥',
											area: ['450px', '350px'], //宽高
											content:'<div style="padding:30px 50px;">\
														<textarea class="bt-input-text" row="4" name="secret_key" style="width:350px;height:200px;line-height:22px;" readonly>'+ res +'</textarea></textarea>\
														<ul style="margin: 15px 5px;padding-left: 15px;list-style-type: disc;line-height: 25px;">\
															<li>此密钥用配置从服务器，配置完成前请勿关闭。</li>\
														</ul>\
													</div>',
											cancel:function(){
												_this.create_table_list();
												_this.check_m_or_s();
											}
										});
									}
								})
								layer.close();
							});
						},success:function(){
							db_html += '<li style="line-height:26px"><label><input type="checkbox" style="margin-right:5px; vertical-align:-2px" value="alldatabases">全部</label></li>'
							for (var i = 0; i < _this.db_List.length; i++) {
								db_html += '<li style="line-height:26px" class="item"><label><input type="checkbox" style="margin-right:5px; vertical-align:-2px" value="'+ _this.db_List[i] +'">'+ _this.db_List[i] +'</label></li>'
							}
							$('#ymlist').html(db_html);
							$('#ymlist input').click(function(event) {
								if($(this).val() == 'alldatabases'){
									if($(this).prop("checked")){
										_this.checkbox_db_list = ['alldatabases'];
										$('#ymlist .item input:checkbox').each(function() { 
											$(this).prop('checked',true);
										});
									}else{
										$('#ymlist input').prop('checked',false);
										_this.checkbox_db_list = [];
									}
								}else{
									_this.checkbox_db_list = [];
									$('#ymlist .item input:checkbox').each(function() {
										if ($(this).prop('checked')) _this.checkbox_db_list.push($(this).val());
									});
									if(_this.checkbox_db_list.length == _this.db_List.length){
										$('#ymlist input').eq(0).prop('checked',true);
										_this.checkbox_db_list = ['alldatabases']
									}else{
										$('#ymlist input').eq(0).prop('checked',false);
									}
								}
							});
						}
					});
				});
			});
			$('.add_slave1').click(function() {
                var db_html = '';
                _this.check_m_or_s(function (res) {
					if (res.status === false) {
						layer.msg("没有设置主从，请先设置后在添加从库", {icon: 2});
					} else {
						masterSlave.loadT[0] = layer.open({
							type: 1,
							closeBtn: 2,
							title: '添加从服务器',
							area: '450px', //宽高
							content: '<form class="bt-form pd20" id="MasterFrom" accept-charset="utf-8">\
										<div class="line">\
											<span class="tname">从库ip地址</span>\
											<div class="info-r c4"><input class="bt-input-text" type="text" name="master_ip" placeholder="请输入从服务器ip地址" value="" style="width:270px"></div>\
										</div>\
										<div class="line">\
											<span class="tname">从库端口</span>\
											<div class="info-r c4"><input class="bt-input-text" type="text" name="master_port" placeholder="请输入从服务器数据库端口" value="3306" style="width:270px"></div>\
											<div class="info-r c4" style="line-height: 30px;color:#999"> * Mysql默认端口为3306，如存在变动，请更改。</div>\
										</div>\
									</form>',
							btn: ['确定', '取消'],
							yes: function () {
								var ipVal = $('input[name="master_ip"]').val();
								var portVal = $('input[name="master_port"]').val();
								if (!_this.ip_reg.test(ipVal)) {
									layer.msg('IP地址格式错误，请重试！');
									return false;
								} else if (!_this.port_reg.test(portVal)) {
									layer.msg('数据库端口格式错误，请重试！');
									return false;
								}
								_this.add_slave({
									slave_ip: ipVal,
									slave_port: portVal,
									success: function (res) {
										layer.close(masterSlave.loadT[0]);
										masterSlave.loadT[0] = layer.open({
											type: 1,
											closeBtn: 2,
											skin: 'layer_close',
											title: '主服务器密钥',
											area: ['450px', '320px'], //宽高
											content: '<div style="padding:30px 50px;">\
														<textarea class="bt-input-text" row="4" name="secret_key" style="width:350px;height:150px;line-height:22px;" readonly>' + res + '</textarea></textarea>\
														<ul style="margin: 15px 5px;padding-left: 15px;list-style-type: disc;line-height: 25px;">\
															<li>此密钥用配置从服务器，配置完成前请勿关闭。</li>\
														</ul>\
													</div>',
											cancel: function () {
												_this.create_table_list();
												_this.check_m_or_s();
											}
										});
									}
								})
							}
						});
					}
                });
			});
			$('.get_key').click(function() {
                _this.get_key({
					success: function (res) {
					    if(res === false){
					        layer.msg("没有设置主从，请先设置后在再获取",{icon:2});
						}else {
                            layer.close(masterSlave.loadT[0]);
                            masterSlave.loadT[0] = layer.open({
                                type: 1,
                                closeBtn: 2,
                                skin: 'layer_close',
                                title: '主服务器密钥',
                                area: ['450px', '420px'], //宽高
                                content: '<div style="padding:30px 50px;">\
											<textarea class="bt-input-text" row="4" name="secret_key" style="width:350px;height:230px;line-height:22px;" readonly>' + res + '</textarea></textarea>\
											<ul style="margin: 15px 5px;padding-left: 15px;list-style-type: disc;line-height: 25px;">\
												<li>此密钥用配置从服务器，配置完成前请勿关闭。</li>\
											</ul>\
										</div>',
                                cancel: function () {
									_this.create_table_list();
                                }
                            });
                        }
					}
				})
            });
			$('.masterslave').on('click','.repair_master_slave',function(){
				var slave_ip = $(this).attr('data-repair');
				_this.repair_testing(slave_ip,function(res){
					layer.open({
						type: 1,
						area: '320px', //宽高
						title:'修复主从复制',
						icon:2,
						closeBtn:2,
						content:'<div style="padding:15px;color:#333;font-size:13px">'+res.msg+'</div>',
						btn:['确定','取消'],
						yes:function(index, layero){
							_this.repair_master_slave(slave_ip,function(res){
								if(res.status){
									layer.close(index);
									_this.create_table_list();
								}
							});
						},
						btn2:function(index, layero){
							layer.close(index);
						}
					})
				})
			});
			$('.masterslave').on('click','.dbs_details_list',function(){
				var title = $(this).attr('title');
				var title_list = title.split('、'),content = '';
				for(var i = 0;i<title_list.length; i++){
					content += '<span>'+ title_list[i] +'</span>'
				}
				masterSlave.loadT[0] = layer.open({
					type: 1,
					closeBtn:2,
					title:'数据库',
					area: '300px', //宽高
					shade:.1,
					content:'<div class="pd15 dbs_layer_view"><div class="list_dbs"><span class="th">数据库名称</span>'+ content +'</div><div class="info_dbs">*&nbsp;显示已设置主从复制的数据库</div></div>',
				})
			});
			$('.server_state .text').click(function(){
				var code = $(this).attr('data-code');
				switch(code){
					case '0':
						// layer.msg('',{icon:0})
					break;
					case '1':
						layer.msg('当前服务器配置“主服务器”,如需切换成从服务器，请删除当前主服务配置信息！',{icon:0,time:5000})
					break;
					case '2':
						layer.msg('当前服务器配置“从服务器”,如需切换成主服务器，请删除当前从服务配置信息！',{icon:0,time:5000})
					break;

				}
			});
			this.check_m_or_s(function(res){
				if(res.status)_this.create_table_list();
			});
		},
	    // 检查服务器是否主服务器
		check_m_or_s:function(callback){
			this.send({
				method:'CheckMasterOrSlave',
				tips:'正在检查主从配置状态，请稍后<img src="/static/img/ing.gif">',
				success:function(res){
					if(!res.status){
						$('.server_state,.masterslave .header').show();
						$('.masterslave .header button:eq(0),.masterslave .header button:eq(1)').show();
						$('.masterslave .header button:eq(2),.masterslave .header button:eq(3)').hide();
						$('.server_state .text').html('未配置服务').attr('data-code',0);
					}else{
						if(res.msg === '该服务器为主服务器'){
							$('.masterslave .header').show();
							$('.masterslave .header button:eq(0),.masterslave .header button:eq(1)').hide();
							$('.masterslave .header button:eq(2),.masterslave .header button:eq(3)').show();
							$('.server_state .text').html('主服务器').attr('data-code',1);
						}else{
							$('.masterslave .header').hide();
							$('.masterslave .header button').show();
							$('.server_state .text').html('从服务器').attr('data-code',2);
						}
					}
					if(callback) callback(res);
				}
			});
		},
		// 获取数据库
		get_dbs:function(callback){
			this.send({
				method:'GetDbs',
				tips:'正在获取数据库列表，请稍后<img src="/static/img/ing.gif">',
				success:function(res){
					if(res.status === false){
						layer.msg(res.msg,{icon:2});
					}else{
						if(callback) callback(res);
					}
				}
			});
		},
		// 创建主服务
		set_master:function(obj){
			this.send({
				method:'SetMaster',
				tips:'正在设置主服务器，请稍后<img src="/static/img/ing.gif">',
				timeout:20000,
				data:{
					slave_ip:obj.slave_ip,
					slave_port:obj.slave_port,
					replicate_dbs:obj.replicate_dbs
				},
				success:function(res){
					if(res.status === false){
						layer.msg(res.msg,{icon:2});
						return false;
					}
					if(obj.success) obj.success(res);
				}
			});
		},
	  	// 添加从库
	  	add_slave:function(obj){
			this.send({
				method:'AddSalve',
				tips:'正在添加从服务器，请稍后<img src="/static/img/ing.gif">',
				timeout:20000,
				data:{
					slave_ip:obj.slave_ip,
					slave_port:obj.slave_port,
				},
				success:function(res){
					if(res.status === false){
						layer.msg(res.msg,{icon:2});
						return false;
					}
					if(obj.success) obj.success(res);
				}
			});
		},
		// 取key
	  	get_key:function(obj){
			this.send({
				method:'GetKeys',
				tips:'正在获取Key，请稍后<img src="/static/img/ing.gif">',
				timeout:20000,
				success:function(res){
					if(res.status === false){
						layer.msg(res.msg,{icon:2});
						return false;
					}
					if(obj.success) obj.success(res);
				}
			});
		},
		// 创建从服务
		set_slave:function(obj){
			this.send({
				method:'SetSlave',
				tips:'正在设置从服务器，请稍后<img src="/static/img/ing.gif">',
				timeout:99999999999999,
				data:{
					master_ip:obj.master_ip,
					keys:obj.keys
				},
				success:function(res){
					layer.msg(res.msg,{icon:res.status?1:2});
					if(obj.success) obj.success(res)
					if(typeof res != 'object'){
						layer.msg(res,{icon:2});
					}
				}
			});
		},
		// 删除主从
		remove_replicate:function(obj){
			var _this = this;
			var str = '是否删除 【'+ obj.slave_ip +'】 Mysql主从复制配置信息?'
			layer.confirm(str,{title:'删除主从复制配置',icon:0,closeBtn: 2,shift: 5},function(){
				_this.send({
					method:'RemoveReplicate',
					data:{
						slave_ip:obj.slave_ip,
					},
					tips:'正在删除主从复制配置信息，请稍后<img src="/static/img/ing.gif">',
					success:function(res){
						_this.create_table_list();
						_this.check_m_or_s();
						layer.msg(res.msg,{icon:res.status?1:2});
					}
				});
			});
		},
		// 获取从服务器进度
		get_slave_speed:function(callback){
			this.send({
				method:'GetSpeed',
				load:2,
				timeout:60*60*1000,
				success:function(res){
					if(callback) callback(res)
				}
			});
		},
		// 获取从服务器总进度
		get_slave_total_speed:function(callback){
			this.send({
				method:'GetTotalSpeed',
				load:2,
				timeout:60*60*1000,
				success:function(res){
					if(callback) callback(res)
				}
			})
		},
		// 获取当前主从配置信息
		get_replicate_satus:function(callback){
			this.send({
				method:'GetReplicateStatus',
				load:1,
				success:function(res){
					if(!res.status){
						layer.msg(res.msg,{icon:2});
						return false;
					}
					callback(res);
				}
			})
		},
		// 创建列表
		create_table_list:function(){
			this.get_replicate_satus(function(res){
				var rdata = res.msg,_html = '';
			    for (var i = 0;i < rdata.length;i++) {
					var item = rdata[i], dbs_list = '';
                    if (typeof item != 'object') {
                        $('.master_slave_list').html('');
                        return false;
                    }
                    if (item.replicate_dbs[0] == 'alldatabases') {
                        dbs_list = '<span title="全部" style="width:100px">全部</div>'
                    } else {
                        for (var j = 0; j < item.replicate_dbs.length; j++) {
                            dbs_list += item.replicate_dbs[j] + (j != item.replicate_dbs.length - 1 ? '、' : '');
                        }
                        dbs_list = '<span style="width:100px"><a href="javascript:;" title="' + dbs_list + '" class="btlink dbs_details_list">' + dbs_list + '</a></span>';
					}

					_html += '<tr><td>' + (item.master_ip == 'local' ? '本地' : item.master_ip) + '</td><td>' + (item.slave_ip == 'local' ? '本地' : item.slave_ip) + '</td><td>' + dbs_list + '</td><td>' + (item.Slave_SQL_Running.toLowerCase() == 'yes' ? '<span>正常</span>' : '<span style="color:red">异常</span>') + '</td><td>' + (item.Slave_IO_Running.toLowerCase() == 'yes' ? '<span >正常</span>' : '<span style="color:red">异常</span>') + '</td><td style="text-align:right;"><a href="javascript:;" class="btlink repair_master_slave" data-repair="'+ item.slave_ip +'" title="修复主从" >修复</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:;" class="btlink" title="删除主从" onclick="masterSlave.remove_replicate({slave_ip:\''+ item.slave_ip +'\'})">删除</a></td></tr>';
				}
				$('.master_slave_list').html(_html);
			});
		},
		// 修复主从检测
		repair_testing:function(slave_ip,callback){
			this.send({
				method:'GetReplicateError',
				tips:'正在检测当前主从配置<img src="/static/img/ing.gif">',
				data:{slave_ip:slave_ip},
				success:function(res){
					if(res.msg == '请到主服务器执行'){
						layer.msg(res.msg,{icon:0});
						return true;
					}
					if(!res.status){
						layer.msg(res.msg,{icon:1});
						return true;
					}
					if(callback) callback(res);
				}
			})
		},
		
		repair_master_slave:function(slave_ip,callback){
			this.send({
				method:'FixReplicate',
				tips:'正在修复当前主从配置<img src="/static/img/ing.gif">',
              	data:{slave_ip:slave_ip},
				timeout:99999999,
				success:function(res){
					layer.msg(res.msg,{icon:res.status?1:2});
					if(callback) callback(res);
				}
			})
		},
		// 发送请求封装
		send:function(obj){
			var loadT = '';
			if(obj.load == undefined) obj.load = 0
			if(obj.plugin_name === undefined && masterSlave.plugin_name !== undefined) obj.plugin_name = masterSlave.plugin_name
			if(!obj.plugin_name || !obj.method){
				layer.msg('插件类名称，或插件方法名称缺失!',{icon:2});
				return false;
			}
			if(obj.load === 0){
				loadT = layer.msg(obj.tips,{icon:16,time:0,shade:0.3});
			}else if(obj.load === 1){
				loadT = layer.load();
			}
			$.ajax({
				type:'POST',
				url: '/plugin?action=a&name=' + obj.plugin_name +'&s='+ obj.method ,
				data: obj.data || {},
				timeout:obj.timeout || 8000,
				complete:function(res){
					console.log(obj.load,loadT);
					if(obj.load === 0 || obj.load === 1) layer.close(loadT);
				},
				success: function(rdata) {
					if (!obj.success) {
						layer.msg(rdata.msg, { icon: rdata.status ? 1 : 2 });
						return;
					}
					obj.success(rdata);
				},
				error: function(ex) {
					if (!obj.error) {
						layer.msg('请求过程发现错误!', { icon: 2 });
						return;
					}
					return obj.error(ex);
				}
			});
		}
  }
  masterSlave.init();
</script>
